// 将 redux 的信息转成后端过滤器的数据结构（@see: https://zapi.in.zhihu.com/project/6212/interface/api/171211）
import { tsFmtLongDate } from '@/utils/time';

import { LogicType } from './filterData';
import getEduShowContent from './getEduShowContent';
import getShowContent from './getShowContent';
import { TagTypeType } from './reduxData';

const reduxInfoToFilterData = (data) => {
  // 将 redux 数据倒成一个过滤器结构的列表
  let tagsList;
  const reduxDataList = data.map((dataItem) => {
    const {
      groupTagData,
      value,
      groupId,
      confidence,
      groupTagInfo,
      id,
      extend_crowdId,
      dimId,
      tags,
    } = dataItem;
    const { groupName, customType } = groupTagInfo || {};
    //标签类
    if (customType === 1) {
      return {
        type: 'custom',
        customId: groupId,
        customName: groupName,
        id: dimId,
        customType,
      };
      //扩展人群
    } else if (customType === 2) {
      const base = {
        type: 'extend_crowd',
        crowdId: extend_crowdId,
        groupName,
      };
      if (confidence) {
        return {
          ...base,
          confidence: Number(confidence),
        };
      }
      return base;
    } else if (customType === 3) {
      return {
        type: 'crowd',
        crowdId: id,
        groupName,
      };
    } else if (customType === 9) {
      //todo 广告投放转化人群 此处groupId === 9999999 && customType === 9 均可 后期看业务扩展可调整
      const {
        dateRange,
        dimensionValue,
        measureValueRangeStart,
        measureValueRangeEnd,
        measureType,
      } = value;
      return {
        ...value,
        groupId,
        customType,
        type: 'biz_tag',
        groupName,
        dimensionValue: dimensionValue.map((item) => item.value),
        dimensionLabel: dimensionValue.map((item) => item.label),
        measureType: +measureType,
        dateRange: [tsFmtLongDate(dateRange[0]), tsFmtLongDate(dateRange[1])],
        measureValueRange: [measureValueRangeStart, measureValueRangeEnd],
        showContent: getShowContent({
          ...value,
          dimensionLabel: dimensionValue.map((item) => item.label),
          groupName,
        }),
      };
    } else if (customType === 10) {
      const { behavior, contains_edu_cards, date_range, subjects } = value;
      const res = {
        ...value,
        groupId,
        customType,
        type: 'biz_tag',
        biz_name: 'edu_content_consumer',
        groupName,
        subjects: subjects.map(({ value }) => value),
        behavior: behavior.map((item) => item.value),
        contains_edu_cards: contains_edu_cards.map((item) => item.value),
        date_range: [
          tsFmtLongDate(date_range[0]),
          tsFmtLongDate(date_range[1]),
        ],
      };
      return {
        ...res,
        showContent: getEduShowContent({ groupName, customType, ...res }),
      };
    } else if (customType === 11) {
      const { behavior, date_range, subjects } = value;
      const res = {
        ...value,
        groupId,
        customType,
        type: 'biz_tag',
        biz_name: 'edu_non_content_consumer',
        groupName,
        subjects: subjects.map(({ value }) => value),
        behavior: behavior.map((item) => item.value),
        date_range: [
          tsFmtLongDate(date_range[0]),
          tsFmtLongDate(date_range[1]),
        ],
      };
      return {
        ...res,
        showContent: getEduShowContent({ groupName, customType, ...res }),
      };
    } else if (customType === 12) {
      const { date_range, subjects } = value;
      const res = {
        ...value,
        groupId,
        customType,
        type: 'biz_tag',
        biz_name: 'edu_sku_consumer',
        groupName,
        subjects: subjects.map(({ value }) => value),
        date_range: [
          tsFmtLongDate(date_range[0]),
          tsFmtLongDate(date_range[1]),
        ],
      };
      return {
        ...res,
        customType,
        showContent: getEduShowContent({ groupName, customType, ...res }),
      };
    } else {
      tagsList =
        tags !== undefined
          ? tags
          : groupTagData
              .map(({ id: tagId, tagName, tagType }) => {
                const curVal = value[tagId];
                if (!curVal) return null;

                let object = {};

                if (tagType === TagTypeType.ENUM) {
                  object = {
                    tagValues: curVal.map((val) => {
                      const { tagDataId, value } = val;
                      return {
                        valueId: tagDataId,
                        valueText: value,
                      };
                    }),
                  };
                }
                if (tagType === TagTypeType.TEXT) {
                  object = {
                    tagValues: curVal.map((val) => {
                      const { label, value } = val;
                      return {
                        valueId: value,
                        valueText: label,
                      };
                    }),
                  };
                }
                if (tagType === TagTypeType.NUMBER) {
                  const number = curVal;
                  object = {
                    number,
                  };
                }
                //ts 的类型推断会觉得后面有 undefined，所以直接最后一种情况做兜底
                if (tagType === TagTypeType.DATERANGE) {
                  const [dateFrom, dateTo] = curVal;
                  object = {
                    dateFrom,
                    dateTo,
                  };
                }

                if (tagType === TagTypeType.RANGE) {
                  const [numberFrom, numberTo] = curVal;
                  object = {
                    numberFrom,
                    numberTo,
                  };
                }

                if (tagType === TagTypeType.Tree) {
                  object = {
                    tagValues: curVal.map((val) => {
                      const { tagDataId, value } = val;
                      return {
                        valueId: tagDataId,
                        valueText: value,
                      };
                    }),
                  };
                }

                if (tagType === TagTypeType.WithCoverage) {
                  object = {
                    tagValues: curVal.map((val) => {
                      const { tagDataId, value } = val;
                      return {
                        valueId: tagDataId,
                        valueText: value,
                      };
                    }),
                  };
                }
                return {
                  tagId,
                  tagType,
                  tagName,
                  ...object,
                };
              })
              .filter(Boolean);
    }
    return {
      type: 'tag',
      groupId,
      groupName,
      customType,
      tags: tagsList,
      tagId: tagsList[0]?.tagId + '' + groupId,
    };
  });

  return {
    type: LogicType.AND,
    filters: [
      {
        type: LogicType.AND,
        // @ts-ignore
        filters: [{ type: LogicType.AND, filters: reduxDataList }],
      },
    ],
  };
};

export default reduxInfoToFilterData;
